<%-- 
    Document   : commandParser
    Created on : 15 Apr 11, 11:25:47
    Author     : dell
--%>

<%@page import="chat.*" %>
<%@page import="bean.*" %>
<%@page import="database.db" %>

<%

    String command = request.getParameter("com");
    ChatRoomList roomList = (ChatRoomList)application.getAttribute("roomList");
    boolean isModerator = false;

    if(command.charAt(0)=='/'){
            String[] commands = command.split(" ");
            
            if(commands[0].equals("/create")){
                //create room
                if(commands.length==2){
                    //bikin room commands[1] di kategori ini
                    application.setAttribute("globalRoomCounter",(Integer)(application.getAttribute("globalRoomCounter"))+1);
                    int id = (Integer)application.getAttribute("globalRoomCounter");
                    roomList.addRoom(new ChatRoom(id, 2, commands[1], null));
                    out.println("room "+commands[1]+" created");
                    out.println("room id = "+id);
                }
                else if(commands.length==3){
                    //bikin room commands[1] di kategori commands[2]
                    application.setAttribute("globalRoomCounter",(Integer)(application.getAttribute("globalRoomCounter"))+1);
                    int id = (Integer)application.getAttribute("globalRoomCounter");
                    roomList.addRoom(new ChatRoom(id, 2, commands[1], null));
                    out.println("room "+commands[1]+" created");
                }
                else
                    out.println("create: command error\n/create <roomname> [kategori]");
            }
            else if(commands[0].equals("/join")){
                //join room
                if(commands.length==2){
                    //masuk ke room commands[1]
                    Chatter c = new Chatter((session.getAttribute("username")).toString(), null, 0);
                    ChatRoom r = roomList.getRoom(commands[1]);
                    if(r != null){
                        r.addChatter(c);
                        application.setAttribute("roomList", roomList);
                        out.println("join room "+commands[1]);
                    }
                    else
                        out.println("room not found");
                }
                else
                    out.println("join: command error\n/join <roomname>");
            }
            else if(commands[0].equals("/chat")){
                //do chat
                if(commands.length==2){
                    //chat dengan user commands[1]
                    application.setAttribute("globalRoomCounter",(Integer)(application.getAttribute("globalRoomCounter"))+1);
                    int id = (Integer)application.getAttribute("globalRoomCounter");
                    Chatter c = new Chatter((session.getAttribute("username")).toString(), null, 0);
                    ChatRoom r = new ChatRoom(id, 1, session.getAttribute("username")+"#"+commands[1], null);
                    r.addChatter(c);
                    application.setAttribute("roomList", roomList);
                    out.println("chat with "+commands[1]);
                }
                else
                    out.println("chat: command error\n/chat <username>");
            }
            else if(commands[0].equals("/addfriend")){
                //add friend
                if(commands.length==2){
                    //menambah user commands[1] sebagai friend
                    db.addfriend((Integer)(session.getAttribute("uid")), db.getUserId(commands[1]));
                    out.println(commands[1]+" is now your friend");
                }
                else
                    out.println("addfriend: command error\n/addfriend <username>");

            }
            else if(commands[0].equals("/exit")){
                //exit room / chat
                int chatRoomNumber = (Integer)(session.getAttribute("selectedRoom"));
                String desiredChatterToRemoveName = session.getAttribute("username").toString();
                ChatRoom room = roomList.getRoomOfId(chatRoomNumber);
                room.removeChatter(desiredChatterToRemoveName);
                application.setAttribute("roomList",roomList);
                out.println("exit room "+commands[1]);
            }
            else if(commands[0].equals("/info")){
                //get information
                if(commands.length==2){
                    //menampilkan informasi user/room commands[1]
                    user u = db.selectuser(commands[1]);
                    if(u != null){
                        out.println("user info:");
                        out.println("Nama     :"+u.getNama());
                        out.println("Username :"+u.getUsername());
                        out.println("Role     :"+db.ismoderator(u.getId()));
                    }
                    ChatRoom c = roomList.getRoom(commands[1]);
                    if(c != null){
                        out.println("room info:");
                        out.println("nama   :"+c.getName());
                    }
                }
                else
                    out.println("info: command error\n/info [roomname|username]");
            }

            //khusus moderator / owner
            else if(commands[0].equals("/kick")){
                //kick user
                if(isModerator){
                    if(commands.length==2){
                        //mengeluarkan user commands[1]
                        int chatRoomNumber = (Integer)(session.getAttribute("selectedRoom"));
                        ChatRoom room = roomList.getRoomOfId(chatRoomNumber);
                        room.removeChatter(commands[1]);
                        application.setAttribute("roomList",roomList);
                        out.println(commands[1]+" kicked");
                    }
                    else
                        out.println("kick: command error\n/kick <username>");
                }
                else
                    out.println("you don't have permission to do this");
            }
            else if(commands[0].equals("/ban")){
                //ban user
                if(isModerator){
                    if(commands.length==2){
                        //ban user commands[1]
                        int chatRoomNumber = (Integer)(session.getAttribute("selectedRoom"));
                        ChatRoom room = roomList.getRoomOfId(chatRoomNumber);
                        room.removeChatter(commands[1]);
                        application.setAttribute("roomList",roomList);
                        out.println(commands[1]+" banned");
                    }
                    else
                        out.println("ban: command error\n/ban <username>");
                }
                else
                    out.println("you don't have permission to do this");
            }
            else if(commands[0].equals("/unban")){
                //unban user
                if(isModerator){
                    if(commands.length==2){
                        //membatalakn ban user commands[1]
                        out.println(commands[1]+" unbanned");
                    }
                    else
                        out.println("unban: command error\n/unban <username>");

                }
                else
                    out.println("you don't have permission to do this");
            }
            else if(commands[0].equals("/persist")){
                //set persistence
                if(isModerator){
                    if(commands.length==2){
                        if(commands[1].equals("on")){
                            //set persist on
                            out.println(" persist : on");
                        }
                        else if(commands[1].equals("off")){
                            //set persist off
                            out.println(" persist : on");
                        }
                        else
                            out.println("persist: command error\n/persist <on|off>");
                    }
                    else
                        out.println("persist: command error\n/persist <on|off>");
                }
                else
                    out.println("you don't have permission to do this");
            }
            else if(commands[0].equals("/setowner")){
                //set owner
                if(isModerator){
                    if(commands.length==2){
                        //mengeset user commands[1] sebagai owner
                        out.println(commands[1]+" is now an owner");
                    }
                    else
                        out.println("setowner: command error\n/setowner <username>");
                }
                else
                    out.println("you don't have permission to do this");
            }
            else
                out.println("command undefined");
        }
        else
            out.println("that's not a command");
%>